package tt.dz.service.imp.sys;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import redis.clients.jedis.Jedis;
import tt.dz.dao.ext.dic.OmsDicCityCodeDao;
import tt.dz.dao.ext.dic.OmsDicDistrictCodeDao;
import tt.dz.dao.ext.dic.OmsDicProvinceCodeDao;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.dic.OmsDicCityCode;
import tt.dz.entity.dic.OmsDicDistrictCode;
import tt.dz.entity.dic.OmsDicProvinceCode;
import tt.dz.service.sys.ProvinceService;
import tt.dz.util.RedisClient;
import tt.dz.util.SerializeUtil;
@Service(value="provinceService") @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProvinceServiceImpl  implements ProvinceService{
	@Resource
	OmsDicProvinceCodeDao provinceCodeDao;
	@Resource
	OmsDicCityCodeDao cityCodeDao;
	@Resource
	OmsDicDistrictCodeDao districtCodeDao;
//	@Override
//	public String getChargerList(OmsCharger charger) {
//		List<OmsCharger> list = omsChargerDao.findExample(charger, null, null, null, null);
////		list.get(0).setUser_pay_card("111111111");
//        JsonForList<OmsCharger>  jList = new  JsonForList<OmsCharger>(); 
//        jList.setCode("200");
//        jList.setMsg("ok");
//        jList.setList(list);
//		String jsonString = JSON.toJSONString(jList);
//		return jsonString;		
//	}
//
//	@Override
//	public String updateCharger(OmsCharger charger) {
//		int result = omsChargerDao.updateObject(charger);
//		JsonCommon jsonC = new JsonCommon();
//		if(result>0){
//			jsonC.setCode("200");
//			jsonC.setMsg("updateCharger ok");
//			String jsonString = JSON.toJSONString(jsonC);
//			return jsonString;	
//		}else{
//			jsonC.setCode("400");
//			jsonC.setMsg("updateCharger fail");
//			String jsonString = JSON.toJSONString(jsonC);
//			return jsonString;	
//		}
//	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public JsonForList getAll() {
		JsonForList json = new JsonForList();
//		byte[] provinceList_byte =  RedisClient.getConnect().get("allArea".getBytes());
		Jedis jedis = RedisClient.getConnect();
		byte[] provinceList_byte = jedis.get("allArea".getBytes());
		jedis.close();
		if(provinceList_byte!=null){
			 List<OmsDicProvinceCode> provinceList_redis =   (List<OmsDicProvinceCode>) SerializeUtil.unserialize(provinceList_byte);
			 json.setCode("200");
			 json.setMsg("ok");
			 json.setList(provinceList_redis);
			 return json;
		}
//		StringBuilder sb = new StringBuilder("");
		//sb.append("select a. from oms_dic_province_code a ,oms_dic_city_code b ,oms_dic_district_code c where ");
		List<OmsDicProvinceCode> provinceList= provinceCodeDao.findAll(new OmsDicProvinceCode());
		for(OmsDicProvinceCode provice : provinceList){
			OmsDicCityCode city_query = new OmsDicCityCode();
			city_query.setProvince_code(provice.getProvince_code());
			List<OmsDicCityCode> cityList= cityCodeDao.findExample(city_query, null, null, null, null);
			for(OmsDicCityCode city : cityList){
				OmsDicDistrictCode district_query = new OmsDicDistrictCode();
				district_query.setCity_code(city.getCity_code());
				List<OmsDicDistrictCode> districtList= districtCodeDao.findExample(district_query, null, null, null, null);
				city.setDistrictList(districtList);
			}
			provice.setCityList(cityList);
		}
		json.setCode("200");
		json.setMsg("ok");
		json.setList(provinceList);
//		RedisClient.getConnect().set("allArea".getBytes(), SerializeUtil.serialize(provinceList));
		Jedis curJedis = RedisClient.getConnect();
		curJedis.set("allArea".getBytes(), SerializeUtil.serialize(provinceList));
		curJedis.close();
		return json;
	}

	@SuppressWarnings("unused")
	@Override
	public JsonForList getProviceAndCityByPinyin() {
		JsonForList json = new JsonForList();
		StringBuilder sb = new StringBuilder("");
		//sb.append("select a. from oms_dic_province_code a ,oms_dic_city_code b ,oms_dic_district_code c where ");
		List<OmsDicProvinceCode> provinceList= provinceCodeDao.findExample(new OmsDicProvinceCode(), null, null, "sort", 1);
		for(OmsDicProvinceCode provice : provinceList){
			OmsDicCityCode city_query = new OmsDicCityCode();
			city_query.setProvince_code(provice.getProvince_code());
			List<OmsDicCityCode> cityList= cityCodeDao.findExample(city_query, null, null, null, null);
			provice.setCityList(cityList);
		}
		json.setCode("200");
		json.setMsg("ok");
		json.setList(provinceList);
		return json;
	}


}
